package io.seata.samples.integration.account.dubbo;

import io.seata.samples.integration.account.service.ITAccountService;
import io.seata.samples.integration.common.account.*;
import io.seata.samples.integration.common.response.ObjectResponse;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;

@GrpcService
@Slf4j
public class AccountDubboServiceImpl extends GreeterGrpc.GreeterImplBase {

    @Autowired
    private ITAccountService accountService;

    @Override
    public void decreaseAccount(AccountRequest request,
          io.grpc.stub.StreamObserver<AccountResponse>
                                        responseObserver) {
//        log.info("全局事务id ：" + RootContext.getXID());

        ObjectResponse objectResponse = accountService.decreaseAccount(request.getUserId(),request.getAmount());

        AccountResponse response = AccountResponse.newBuilder()
                .setStatus(objectResponse.getStatus())
                .setMessage(objectResponse.getMessage())
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    @Override
    public void getAccountLeft(com.google.protobuf.Empty request,
                               io.grpc.stub.StreamObserver<AccountLeftResponse> responseObserver){
        Double accountLeft = accountService.getAccountLeft();

        AccountLeftResponse accountLeftResponse = AccountLeftResponse.newBuilder()
                .setLeft(accountLeft)
                .build();
        responseObserver.onNext(accountLeftResponse);
        responseObserver.onCompleted();
    }

    /*@Override
    public ObjectResponse getAccountLeft() {
        ObjectResponse objectResponse = new ObjectResponse();
        objectResponse.setData(accountService.getAccountLeft());
        return objectResponse;
    }*/
}
